//
//  TanHunHeaderView.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TanHunHeaderView.h"
#import "ThemePostCell.h"

@interface TanHunHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>



@end

@implementation TanHunHeaderView



+ (CGFloat)calculateHeight {     
    return 17+60+22 + (TTScreenWidth - 20)*0.1613 + 52;//56:347
}

- (void)fillWith:(NSMutableArray<CommunityThemeModel *>*)themeArray {
    
    self.themeArray = @[].mutableCopy;
    self.themeArray = themeArray;
    [self.collectionview reloadData];
}

- (IBAction)searchAction:(id)sender {
    if (self.TanHunHeaderViewBlock) {
        self.TanHunHeaderViewBlock(TanHunHeaderViewActionType_wenda,-1);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];

    [self initView];
}

- (void)initView {
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLaout.minimumInteritemSpacing = 8;
    flowLaout.minimumLineSpacing = 8;
    
    flowLaout.itemSize = CGSizeMake(90, 60);

    self.collectionview.collectionViewLayout = flowLaout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"TanHunTopicCell" bundle:nil] forCellWithReuseIdentifier:@"TanHunTopicCellID"];
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"ThemePostCell" bundle:nil] forCellWithReuseIdentifier:@"ThemePostCellID"];

    //默认最热选中
    [self.hotestBtn setTitleColor:UIColorHex(0xff4242) forState:UIControlStateNormal];
    self.hotestBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];

    [self.collectionview reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        ThemePostCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThemePostCellID" forIndexPath:indexPath];
        return cell;
        
    } else {
        
        TanHunTopicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TanHunTopicCellID" forIndexPath:indexPath];
        [cell fillWithModel:self.themeArray[indexPath.row]];
        return cell;
    }
    

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.themeArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.TanHunHeaderViewBlock) {
        self.TanHunHeaderViewBlock(TanHunHeaderViewActionType_tiezi,indexPath.row);
    }
}

- (IBAction)hotAction:(id)sender {
    
    self.hotestBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.newestBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];

    [self.hotestBtn setTitleColor:UIColorHex(0xff4242) forState:UIControlStateNormal];
    [self.newestBtn setTitleColor:UIColorHex(0x666666) forState:UIControlStateNormal];

    if (self.TanHunHeaderViewBlock) {
        self.TanHunHeaderViewBlock(TanHunHeaderViewActionType_hot,-1);
    }
}

- (IBAction)newAction:(id)sender {
    
    self.newestBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.hotestBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.newestBtn setTitleColor:UIColorHex(0xff4242) forState:UIControlStateNormal];
    [self.hotestBtn setTitleColor:UIColorHex(0x666666) forState:UIControlStateNormal];

    if (self.TanHunHeaderViewBlock) {
        self.TanHunHeaderViewBlock(TanHunHeaderViewActionType_new,-1);
    }
    
}


@end
